/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.auth.check.context.model;

import com.je.auth.check.AuthCheckEngine;
import com.je.auth.check.exception.TokenException;
import com.je.auth.check.util.FoxUtil;

/**
 * Request 包装类
 *
 * @author kong
 */
public interface AuthRequest {

    /**
     * 获取认证引擎
     *
     * @return
     */
    AuthCheckEngine getAuthCheckEngine();

    /**
     * 提供认证引擎
     *
     * @param authCheckEngine
     */
    void setAuthCheckEngine(AuthCheckEngine authCheckEngine);

    /**
     * 获取底层源对象
     *
     * @return see note
     */
    Object getSource();

    /**
     * 在 [请求体] 里获取一个值
     *
     * @param name 键
     * @return 值
     */
    String getParam(String name);

    /**
     * 在 [请求体] 里获取一个值，值为空时返回默认值
     *
     * @param name         键
     * @param defaultValue 值为空时的默认值
     * @return 值
     */
    default String getParam(String name, String defaultValue) {
        String value = getParam(name);
        if (FoxUtil.isEmpty(value)) {
            return defaultValue;
        }
        return value;
    }

    /**
     * 检测提供的参数是否为指定值
     *
     * @param name  键
     * @param value 值
     * @return 是否相等
     */
    default boolean isParam(String name, String value) {
        String paramValue = getParam(name);
        return FoxUtil.isNotEmpty(paramValue) && paramValue.equals(value);
    }

    /**
     * 检测请求是否提供了指定参数
     *
     * @param name 参数名称
     * @return 是否提供
     */
    default boolean hasParam(String name) {
        return FoxUtil.isNotEmpty(getParam(name));
    }

    /**
     * 在 [请求体] 里获取一个值 （此值必须存在，否则抛出异常 ）
     *
     * @param name 键
     * @return 参数值
     */
    default String getParamNotNull(String name) {
        String paramValue = getParam(name);
        if (FoxUtil.isEmpty(paramValue)) {
            throw new TokenException("缺少参数：" + name);
        }
        return paramValue;
    }

    /**
     * 在 [请求头] 里获取一个值
     *
     * @param name 键
     * @return 值
     */
    String getHeader(String name);

    /**
     * 在 [请求头] 里获取一个值
     *
     * @param name         键
     * @param defaultValue 值为空时的默认值
     * @return 值
     */
    default String getHeader(String name, String defaultValue) {
        String value = getHeader(name);
        if (FoxUtil.isEmpty(value)) {
            return defaultValue;
        }
        return value;
    }

    /**
     * 在 [Cookie作用域] 里获取一个值
     *
     * @param name 键
     * @return 值
     */
    String getCookieValue(String name);

    /**
     * 返回当前请求path (不包括上下文名称)
     *
     * @return see note
     */
    String getRequestPath();

    /**
     * 返回当前请求path是否为指定值
     *
     * @param path path
     * @return see note
     */
    default boolean isPath(String path) {
        return getRequestPath().equals(path);
    }

    /**
     * 返回当前请求的url，不带query参数，例：http://xxx.com/test
     *
     * @return see note
     */
    String getUrl();

    /**
     * 返回当前请求的类型
     *
     * @return see note
     */
    String getMethod();

    /**
     * 此请求是否为Ajax请求
     *
     * @return see note
     */
    default boolean isAjax() {
        return getHeader("X-Requested-With") != null;
    }

    /**
     * 转发请求
     *
     * @param path 转发地址
     * @return 任意值
     */
    default Object forward(String path) {
        throw new TokenException("No implementation");
    }

}
